package TangkGame01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class Panel extends JPanel implements KeyListener , Runnable{
    Hero hero = null;//定义我的坦克
    Vector<EnyTank> enyTanks = new Vector<>();
    Vector<Bomb> bombs = new Vector<>();
    int enyTankssize = 3;

    //三张爆炸图片
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;

    public Panel(){
        hero = new Hero(500 ,100);

        //绘制敌方坦克
        for (int i = 0; i < enyTankssize; i++) {
            EnyTank enyTank = new EnyTank(100 * (i + 1), 0);
            enyTank.direct = 2;
            new Thread(enyTank).start();
            Shot shot = new Shot(enyTank.x + 20, enyTank.y + 60, enyTank.direct);
            enyTank.shots.add(shot);
            new Thread(shot).start();
            enyTanks.add(enyTank);
        }

        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.black);
        g.fillRect(0,0,1000,750);

        //画出我的坦克
        if (hero != null && hero.isLive) {
            DrawTank(hero.x, hero.y, hero.direct, 1, g);
        }
        //画出敌方坦克
        //1.先将坦克从集合中取出来
        for (int i = 0; i < enyTanks.size(); i++) {
            EnyTank enyTank = enyTanks.get(i);
            if (enyTank.isLive) {
                DrawTank(enyTank.x, enyTank.y, enyTank.direct, 0, g);
                for (int j = 0; j < enyTank.shots.size(); j++) {
                    Shot shot = enyTank.shots.get(j);
                    if (shot.isLive) {
                        g.draw3DRect(shot.x, shot.y, 1, 1, false);
                    } else {
                        enyTank.shots.remove(shot);
                    }
                }
            }
        }

        //画出子弹
//        if (hero.shot != null && hero.shot.isLive){
////            g.setColor(Color.RED);
//            g.draw3DRect(hero.shot.x,hero.shot.y,1,1,false);
//        }
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive){
                g.draw3DRect(shot.x,shot.y,1,1,false);
            }else {
                hero.shots.remove(shot);
            }
        }

        //画图图片
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6){
                g.drawImage(image1,bomb.x,bomb.y,60,60,this);
            }else if (bomb.life >3){
                g.drawImage(image2,bomb.x,bomb.y,60,60,this);
            }else {
                g.drawImage(image3,bomb.x,bomb.y,60,60,this);
            }
            bomb.BombDown();
            if (bomb.life == 0){
                bombs.remove(bomb);
            }
        }

    }

    public void DrawTank(int x , int y ,int direct ,int type , Graphics g){
        //坦克需要根据方向来绘制

        switch (type){//0表示蓝色敌方坦克,1表示我方黄色坦克
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }

        switch (direct){
            case 0:
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
                g.fillOval(x + 10, y + 20, 20, 20);//画出圆形盖子
                g.drawLine(x + 20, y + 30, x + 20, y);//画出炮筒
                break;
            case 1: //表示向右
                g.fill3DRect(x, y, 60, 10, false);//画出坦克上边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克下边轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
                g.fillOval(x + 20, y + 10, 20, 20);//画出圆形盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//画出炮筒
                break;
            case 2: //表示向下
                g.fill3DRect(x, y, 10, 60, false);//画出坦克左边轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
                g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
                g.fillOval(x + 10, y + 20, 20, 20);//画出圆形盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//画出炮筒
                break;
            case 3: //表示向左
                g.fill3DRect(x, y, 60, 10, false);//画出坦克上边轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克下边轮子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
                g.fillOval(x + 20, y + 10, 20, 20);//画出圆形盖子
                g.drawLine(x + 30, y + 20, x, y + 20);//画出炮筒
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //控制我的坦克移动
        if (e.getKeyCode() == KeyEvent.VK_W){
            hero.direct = 0;
            if (hero.y > 0 ){
                hero.moveUp();
            }
        }else if (e.getKeyCode() == KeyEvent.VK_D) {//D键, 向右
            hero.direct = 1;
            if (hero.x + 60 < 1000) {
                hero.moveRight();
            }

        } else if (e.getKeyCode() == KeyEvent.VK_S) {//S键
            hero.direct = 2;
            if (hero.y + 60 < 750) {
                hero.moveDown();
            }
        } else if (e.getKeyCode() == KeyEvent.VK_A) {//A键
            hero.direct = 3;
            if (hero.x > 0) {
                hero.moveLeft();
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_J){
//            if (hero.shot == null || !hero.shot.isLive) {
//                hero.shotEnemyTank();
//            }
            //发射多颗子弹
            hero.shotEnemyTank();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hitEnemyTank();
            hitHero();
            this.repaint();
        }
    }

    public void hitTank(Shot s, Tank enemyTank) {
        //判断s 击中坦克
        switch (enemyTank.direct) {
            case 0: //坦克向上
            case 2: //坦克向下
                if (s.x > enemyTank.x && s.x < enemyTank.x + 40
                        && s.y > enemyTank.y && s.y < enemyTank.y + 60) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    //当我的子弹击中敌人坦克后，将enemyTank 从Vector 拿掉
                    enyTanks.remove(enemyTank);
                    //创建Bomb对象，加入到bombs集合
                    Bomb bomb = new Bomb(enemyTank.x, enemyTank.y);
                    bombs.add(bomb);
                }
                break;
            case 1: //坦克向右
            case 3: //坦克向左
                if (s.x > enemyTank.x && s.x < enemyTank.x + 60
                        && s.y > enemyTank.y && s.y < enemyTank.y + 40) {
                    s.isLive = false;
                    enemyTank.isLive = false;
                    enyTanks.remove(enemyTank);
                    //创建Bomb对象，加入到bombs集合
                    Bomb bomb = new Bomb(enemyTank.x, enemyTank.y);
                    bombs.add(bomb);
                }
                break;
        }
    }

    public void hitEnemyTank(){
//        if (hero.shot != null && hero.shot.isLive ){
//            for (int i = 0; i < enyTanks.size(); i++) {
//                EnyTank enyTank = enyTanks.get(i);
//                hitTank(hero.shot, enyTank);
//            }
//        }
        for (int i = 0; i < hero.shots.size(); i++) {
            Shot shot = hero.shots.get(i);
            if (shot != null && shot.isLive ){
                for (int j = 0; j < enyTanks.size(); j++) {
                    EnyTank enyTank = enyTanks.get(j);
                    hitTank(shot, enyTank);
                }
            }
        }
    }

    public void hitHero(){
        for (int i = 0; i < enyTanks.size(); i++) {
            EnyTank enyTank = enyTanks.get(i);
            for (int j = 0; j < enyTank.shots.size(); j++) {
                Shot shot = enyTank.shots.get(j);
                if (shot.isLive && hero.isLive){
                    hitTank(shot,hero);
                }
            }
        }
    }
}
